$(function(){
	//对页面中的元素进行抽取.
	//方法太凌乱,统一管理
	//按钮在JS统一进行监听
	var cc =0;
	var classinfoDatagrid,classinfoEditBtnAndQuitBtn,classinfoDialog,classinfoForm,classinfoSearchBtn,swithBtn  ;
	classinfoDatagrid = $("#classinfo_datagrid");
	classinfoEditBtnAndQuitBtn = $("#classinfo_editBtn,#classinfo_deleteBtn");
	classinfoDialog = $("#classinfo_dialog");
	classinfoForm = $("#classinfo_form");
	classinfoSearchBtn = $("#searchBtn");
	swithBtn = $("#swithBtn");
	//数据表格
	classinfoDatagrid.datagrid({
		fit:true,
		rownumbers:true,
		singleSelect:true,
		pagination:true,
		url:'/classinfo/list',
		fitColumns:true,
		toolbar:'#classinfo_datagrid_tb',
		columns:[
			[
				{field:'title',align:'center',width:10,title:'班级名称'},
                {field:'teacher',align:'center',width:10,formatter:teacherFormatter,title:'班主任'},
                {field:'institute',align:'center',width:10,formatter:instituteFormatter,title:'归属学院'},
                {field:'timetableState',align:'center',width:10,formatter:timetableStateFormatter,title:'课程表'}
			]
		]
	});
	//switch开关按钮
	swithBtn.switchbutton({
		onChange:function (checked){
			var rowData = classinfoDatagrid.datagrid("getSelected");
			if (rowData) {
				if (checked == true) {
					cc=0;
					rowData.timetableState = 0;
				} else if (checked == false) {
					cc=1;
					rowData.timetableState = 1;
				}
			}
		}
	});
	//对话框
	classinfoDialog.dialog({
		width:250,
		height:250,
		buttons:'#classinfo_dialog_bt',
		closed:true
	});
    classinfoSearchBtn.textbox({
		width:230,
		label:"关键字:",
        labelWidth:50,
        prompt:"请输入搜索关键字",
        buttonText:'搜索',
        buttonIcon:'icon-search',
        onClickButton:function(){
			var keyword = $(this).val();
            classinfoDatagrid.datagrid("load",{
            	keyword:keyword
			});
		}
    });
	//对按钮进行统一事件监听
	$("a[data-cmd]").on("click",function(){
		var cmd = $(this).data("cmd");
		if(cmd){
			cmdObj[cmd]();
		}
	});
	//方法统一管理起来]
	var cmdObj = {
		reload:function(){
			//刷新数据表格
			classinfoDatagrid.datagrid("reload");
		},
		cancel:function(){
			classinfoDialog.dialog("close");
		},
		add: function () {
			//1.清空表单数据
			classinfoForm.form("clear");
			//2.设置对话框的标题
			classinfoDialog.dialog("setTitle", "新增");
			//3.打开对话框
			classinfoDialog.dialog("open");
		},
		edit: function () {
			var rowData = classinfoDatagrid.datagrid("getSelected");
			if (rowData) {
				//1.清空表单数据
				classinfoForm.form("clear");
				//2.设置对话框的标题
				classinfoDialog.dialog("setTitle", "编辑");
				//3.打开对话框
				classinfoDialog.dialog("open");
				//特殊数据的处理
				//4.回显数据
				if (rowData.teacher) {
					rowData["teacher.id"] = rowData.teacher.id;
				}
				if (rowData.institute) {
					rowData["institute.id"] = rowData.institute.id;
				}
				if (rowData.timetableState==0) {
					swithBtn.switchbutton("check");
				}else if (rowData.timetableState==1) {
					swithBtn.switchbutton("uncheck");
				}
				classinfoForm.form("load", rowData);//基于同名匹配规则
			} else {
				$.messager.alert("温馨提示", "请选择一条需要修改的数据", "warning");
			}
		},
		save:function(){
			var url;
			var idVal = $("[name='id']").val();
			if(idVal){
				url = "/classinfo/update";
			}else{
				url = "/classinfo/save";
			}
			classinfoForm.form("submit",{
				url:url,
				onSubmit:function (param) {
					param['timetableState']=cc;
					return true;
				},
				success:function(data){
					data = $.parseJSON(data);
					if(data.success){
						//提示消息,当点确定的时候,关闭对话框,刷新数据表格
						$.messager.alert("温馨提示",data.msg,"info",function(){
							classinfoDialog.dialog("close");
							classinfoDatagrid.datagrid("reload");
						});
					}else{
						$.messager.alert("温馨提示",data.msg,"error");
					}
				}
			});
		},
		delete:function(){
			var rowData = classinfoDatagrid.datagrid("getSelected");
			if(rowData){
				$.messager.confirm("温馨提示","您确定需要删除该班级吗?",function(yes){
					if(yes){
						$.get("/classinfo/delete?id="+rowData.id,function(data){
							if(data.success){
								classinfoDatagrid.datagrid("reload");
								$.messager.alert("温馨提示",data.msg,"info");
							}else{
								$.messager.alert("温馨提示",data.msg,"error");
							}
						},"json")
					}
				});
			}else{
				$.messager.alert("温馨提示","请选择需要删除的班级","warning");
			}
		}
	}
});
function teacherFormatter(value,record,index){
	if(value){
		return value.realname;
	}
	return value;
}
function instituteFormatter(value,record,index){
	if(value){
		return value.name;
	}
	return value;
}
function timetableStateFormatter(value,record,index){
	if(value==0){
		return "正常";
	}else if(value==1){
		return "停用";
	}
}

